/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

/* eslint-disable no-useless-escape */
/* eslint-disable no-restricted-globals */
import React from 'react';
import { Modal, Form, Row, Col, InputNumber, Divider, message } from 'antd';

const FormItem = Form.Item;

const limitDecimals = value => {
  const reg = /^(\-)*(\d+)\.(\d\d).*$/;
  if (typeof value === 'string') {
    return !isNaN(Number(value)) ? value.replace(reg, '$1$2.$3') : '';
  }
  if (typeof value === 'number') {
    return !isNaN(value) ? String(value).replace(reg, '$1$2.$3') : '';
  }
  return '';
};

const FeeDistributeModal = props => {
  const { form, type, onOk, onCancel, visible, feeWait } = props;
  const { getFieldDecorator, validateFields } = form;

  // eslint-disable-next-line consistent-return
  const renderForm = formType => {
    const formItemLayout = {
      labelCol: {
        xs: { span: 12 },
        sm: { span: 4 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
    };
    switch (
      formType // 单独修改保管费
    ) {
      case 'distribution': // 单独发放
        return (
          <Form {...formItemLayout}>
            <Row gutter={[8, 8]}>
              <Col>
                所选区间应发金额：
                {feeWait}元
              </Col>
            </Row>
            <Divider />
            <FormItem style={{ marginBottom: 0 }} label="核减金额：">
              <Row gutter={[8, 8]} type="flex" align="middle">
                <Col>
                  {getFieldDecorator('deductFee', {
                    initialValue: '',
                    rules: [
                      {
                        pattern: /^(([0-9]+)|([0-9]+\.[0-9]{1,2}))$/,
                        message: '请输入正确的格式，最多保留两位小数！',
                      },
                    ],
                  })(
                    <InputNumber
                      min={0}
                      step={0.01}
                      formatter={limitDecimals}
                      parser={limitDecimals}
                      style={{ width: 150 }}
                      placeholder="请输入核减金额"
                    />,
                  )}
                </Col>
                <Col>元</Col>
              </Row>
            </FormItem>
            <Row gutter={[8, 8]}>
              <Col>注：核减金额将直接从下属驿站未发金额中扣除。</Col>
            </Row>
            <Divider />
            <FormItem style={{ marginBottom: 0 }} label="发放金额：">
              <Row gutter={[8, 8]} type="flex" align="middle">
                <Col>
                  {getFieldDecorator('sendFee', {
                    initialValue: '',
                    rules: [
                      {
                        pattern: /^(([0-9]+)|([0-9]+\.[0-9]{1,2}))$/,
                        message: '请输入正确的格式，最多保留两位小数！',
                      },
                    ],
                  })(
                    <InputNumber
                      min={0}
                      step={0.01}
                      formatter={limitDecimals}
                      parser={limitDecimals}
                      style={{ width: 150 }}
                      placeholder="请输入发放金额"
                    />,
                  )}
                </Col>
                <Col>元</Col>
              </Row>
            </FormItem>
            <Row gutter={[8, 8]}>
              <Col>注：必须按月全部发放，即核减金额+发放金额必须等于未发金额。</Col>
            </Row>
          </Form>
        );
      case 'giveOutBanch': // 批量发放
        return (
          <Form>
            <FormItem>
              <Row gutter={[8, 8]}>
                <Col>
                  <span style={{ fontWeight: 'bold' }}>发放金额：</span>
                  {getFieldDecorator('releaseBanchFee')(<span>{feeWait}</span>)}元
                </Col>
                <Divider />
                <Col>
                  注：发放后将从快宝智慧快递管理平台账户余额扣除对应的发放金额。发放保管费将根据发放金额额外从快宝智慧快递管理平台账户上扣除1%的手续费。
                </Col>
              </Row>
            </FormItem>
          </Form>
        );
      default:
        break;
    }
  };

  const handelOk = () => {
    // 单独发放需验证
    if (type === 'distribution') {
      validateFields((err, { deductFee, sendFee }) => {
        if (err) return;
        const formatValue = parseFloat((sendFee * 1 + deductFee * 1).toFixed(2));
        if (formatValue !== feeWait * 1) {
          message.error('核减金额+发放金额必须等于应发金额');
        } else {
          onOk && onOk({ deductFee, sendFee });
        }
      });
    } else {
      onOk && onOk();
    }
  };

  const handleCancel = () => {
    onCancel && onCancel();
  };

  return (
    <Modal
      title="保管费发放"
      visible={visible}
      onOk={handelOk}
      onCancel={handleCancel}
      centered
      destroyOnClose
    >
      {renderForm(type)}
    </Modal>
  );
};

export default FeeDistributeModal;
